<template>

    <van-button
            class="follow-btn"
            round
            size="small"
            :loading="loading"
            v-if="isFollowed"
            @click="onFollow"
          >已关注</van-button>
          <van-button
          v-else
            class="follow-btn"
            type="info"
            color="#3296fa"
            round
            size="small"
            icon="plus"
            @click="onFollow"
            >关注</van-button>

</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'followUser',
  model: {
    prop: 'isFollowed',
    event: 'update-is_followed'
  },
  props: {
    isFollowed: {
      type: Boolean,
      require: true
    },
    userId: {
      type: [Number, String, Object],
      require: true
    }
  },
  data () {
    return {
      loading: false
    }
  },

  mounted () {

  },

  methods: {
    async onFollow () {
      this.loading = true // 关注按钮的loading
      // 不知道为啥，一直显示传递参数有问题，明明传的就是用户id，找了半天没找到原因，放弃了，但取消关注正常就很奇怪
      // 猜测可能是因为接口中只有一个自己的用户信息了，所以才会错误，不知道具体原因，如果发现请告诉我
      try {
        if (this.isFollowed) {
          // 已关注 取消
          const { data } = await deleteFollow(this.userId)
          console.log(data)
        } else {
          // 没有关注 点击关
          const { data } = await addFollow(this.userId)
          console.log(data)
        }

        this.$emit('update-is_followed', !this.isFollowed)
      } catch (err) {
        let message = '操作失败，请重试'
        if (err.response && err.response.status === 400) {
          message = '你不能关注你自己'
        }
        this.$toast(message)
      }
      this.loading = false
      this.followLoading = false
    }
  }
}
</script>

<style lang="less" scoped>

</style>
